// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html

var Global = require('global');

cc.Class({
    extends: cc.Component,

    properties: {
        // foo: {
        //     // ATTRIBUTES:
        //     default: null,        // The default value will be used only when the component attaching
        //                           // to a node for the first time
        //     type: cc.SpriteFrame, // optional, default is typeof default
        //     serializable: true,   // optional, default is true
        // },
        // bar: {
        //     get () {
        //         return this._bar;
        //     },
        //     set (value) {
        //         this._bar = value;
        //     }
        // },
        hp:1,
        speed:1,
        score:5,
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start () {

    },
    onCollisionEnter(other,self) {
        //cc.log('敌人碰撞');

        if(other.node.group=='hero'){
            cc.log('游戏结束');
            var anim = other.node.getComponent(cc.Animation);
            anim.play('hero0');
            anim.on('finished',function(e){
                cc.director.loadScene('over');
            },this);
        }else{
            //移除子弹
            other.node.removeFromParent();
            //敌人减血,主角加分
            if(this.hp>0)this.hp -= 1;
            
            if(this.hp==0){
                this.bomb();
            }
            
        }
    },
    bomb(){
        this.node.group = 'default';

        var score = this.node.parent.getChildByName('score').getComponent(cc.RichText);
        Global.score = Global.score + this.score;
        score.string = "<color=#333333>"+Global.score+"</color>";
        
        var anim = this.node.getComponent(cc.Animation);
        anim.play();
        anim.on('finished',function(e){
            this.node.opacity  = 0;
        },this);

        var audio = this.node.getComponent(cc.AudioSource);
        audio.play();

        this.schedule(function(){
            this.node.removeFromParent();
        }, 3);
    },

    // update (dt) {},
});
